#include <stdio.h>  
#include <string.h>   

#include "includes.h"
#include "amsysdef.h"
#include <chipsupport.h>
#include "aw_windows.h"
#include "user.h"
#include <Drivers/audio/audiolib.h>
#ifdef ENABLE_NANDDRV
#include <Drivers/block/nand/nand_drv.h> //added by yangbin

#endif
#ifdef ENABLE_SPIF_FLASH
#include <Drivers/block/spi/spi_drv.h> 
#endif
#include "os/ioapi/libblock/include/ramdisk.h"
#include "sys_app.h"
#include <filesystem/extend/fileio.h>
//#include "i2c.h"
#if (defined AML_ARES)||(defined AML_COPIA)||(defined AML_NIKE)
#ifdef ARES_SOFT_I2C
#include "Drivers/i2c/soft_i2c.h"
#else
#include "Drivers/i2c/hard_i2c.h"
#endif 
#else 
#include "Drivers/i2c/soft_i2c.h"
 #endif

#include "gui/aw_init_display.h"
#include <bsp.h>
#ifdef ENABLE_CARD_READER
#include <cardreader.h>
#endif

#ifdef ENABLE_WATCHDOG
#include <Drivers/watchdog/watchdog.h>
#endif
#ifdef ENABLE_BLUETOOTH
#include <drivers/bluetooth/bt_customer.h>
#endif

//#include "playcmd.h"
//#define ENABLE_DOSFS
#define ENABLE_MPEG2_DEC
#define ENABLE_MPEG4_DEC

#ifdef ENABLE_HDDRV
avfs_device_driver hddisk_initialize(avfs_device_major_number major, avfs_device_minor_number minor, void *arg);
#endif 

#ifdef ENABLE_USBDRV
#include <drivers/usb/usb_inc.h>
avfs_device_driver usb_ms_initialize(avfs_device_major_number major, avfs_device_minor_number minor, void *arg);
#ifdef ENABLE_PTP
#include <drivers/usb/image/ptp_host.h>
#endif
#ifdef ENABLE_USBGADGET
#include <drivers/usb/gadget/gadget.h>
#endif

#endif 

#ifdef ENABLE_RS232
#include <drivers/serial/rs232.h>
#endif

#ifdef ENABLE_DOSFS
unsigned ramdisk_buffer[512*64/4];
avfs_ramdisk_config avfs_ramdisk_configuration[]=
{
 {512,64, (void*)ramdisk_buffer}
};
int avfs_ramdisk_configuration_size =1 ;
#endif
/****************************************************************
 I/O subsystem
****************************************************************/
#ifdef ENABLE_ATAPI
avfs_device_driver dvdrom_initialize(avfs_device_major_number major, avfs_device_minor_number minor, void *arg);
#endif
avfs_device_driver hiu_initialize( avfs_device_major_number major, avfs_device_minor_number minor,void * arg);
avfs_device_driver hiu_open( avfs_device_major_number major, avfs_device_minor_number minor,void * arg);
avfs_device_driver hiu_control( avfs_device_major_number major, avfs_device_minor_number minor,void * arg);
avfs_device_driver hiu_close( avfs_device_major_number major, avfs_device_minor_number minor,void * arg);

avfs_device_driver displayctl_initialize( avfs_device_major_number major, avfs_device_minor_number minor,void * arg);
avfs_device_driver displayctl_control ( avfs_device_major_number major, avfs_device_minor_number minor,void * arg);
avfs_device_driver video_initialize( avfs_device_major_number major, avfs_device_minor_number minor,void * arg);
avfs_device_driver video_control( avfs_device_major_number major, avfs_device_minor_number minor,void * arg);
avfs_device_driver jpegdec_initialize( avfs_device_major_number major, avfs_device_minor_number minor,void * arg);
avfs_device_driver jpegdec_control( avfs_device_major_number major, avfs_device_minor_number minor,void * arg);
avfs_device_driver hwjpegdec_initialize( avfs_device_major_number major, avfs_device_minor_number minor,void * arg);
avfs_device_driver hwjpegdec_control( avfs_device_major_number major, avfs_device_minor_number minor,void * arg);
avfs_device_driver tiffdec_initialize( avfs_device_major_number major, avfs_device_minor_number minor,void * arg);
avfs_device_driver tiffdec_control( avfs_device_major_number major, avfs_device_minor_number minor,void * arg);
avfs_device_driver bmpdec_initialize( avfs_device_major_number major, avfs_device_minor_number minor,void * arg);
avfs_device_driver bmpdec_control( avfs_device_major_number major, avfs_device_minor_number minor,void * arg);
avfs_device_driver bmpdec_write(avfs_device_major_number major, avfs_device_minor_number minor, void * arg);
avfs_device_driver pngdec_initialize( avfs_device_major_number major, avfs_device_minor_number minor,void * arg);
avfs_device_driver pngdec_control( avfs_device_major_number major, avfs_device_minor_number minor,void * arg);
avfs_device_driver pngdec_write(avfs_device_major_number major, avfs_device_minor_number minor, void * arg);
avfs_device_driver gifdec_initialize( avfs_device_major_number major, avfs_device_minor_number minor,void * arg);
avfs_device_driver gifdec_control( avfs_device_major_number major, avfs_device_minor_number minor,void * arg);
avfs_device_driver gifdec_write(avfs_device_major_number major, avfs_device_minor_number minor, void * arg);
avfs_device_driver nulldec_initialize( avfs_device_major_number major, avfs_device_minor_number minor,void * arg);
avfs_device_driver nulldec_control( avfs_device_major_number major, avfs_device_minor_number minor,void * arg);
avfs_device_driver mpeg2dec_initialize( avfs_device_major_number major, avfs_device_minor_number minor,void * arg);
avfs_device_driver mpeg2dec_control( avfs_device_major_number major, avfs_device_minor_number minor,void * arg);
avfs_device_driver mjpegdec_initialize( avfs_device_major_number major, avfs_device_minor_number minor,void * arg);
avfs_device_driver mjpegdec_control( avfs_device_major_number major, avfs_device_minor_number minor,void * arg);
avfs_device_driver mpeg4dec_initialize( avfs_device_major_number major, avfs_device_minor_number minor,void * arg);
avfs_device_driver mpeg4dec_control( avfs_device_major_number major, avfs_device_minor_number minor,void * arg);
#ifdef SUPPORT_RM
avfs_device_driver realdec_initialize( avfs_device_major_number major, avfs_device_minor_number minor,void * arg);
avfs_device_driver realdec_control( avfs_device_major_number major, avfs_device_minor_number minor,void * arg);
#endif

#ifdef ENABLE_NANDDRV
avfs_device_driver nand_initialize( avfs_device_major_number major, avfs_device_minor_number minor,void * arg);//added by yangbin
avfs_device_driver nand_open( avfs_device_major_number major, avfs_device_minor_number minor,void * arg);//added by yangbin
avfs_device_driver nand_close( avfs_device_major_number major, avfs_device_minor_number minor,void * arg);//added by yangbin
avfs_device_driver nand_ioctl( avfs_device_major_number major, avfs_device_minor_number minor,void * arg);//added by yangbin
#endif
#ifdef ENABLE_NETWORK
avfs_device_driver avfs_netdisk_generic_open(avfs_device_major_number major, avfs_device_minor_number minor, void *arg);
avfs_device_driver avfs_netdisk_generic_close(avfs_device_major_number major, avfs_device_minor_number minor, void *arg);
avfs_device_driver avfs_netdisk_generic_read(avfs_device_major_number major, avfs_device_minor_number minor, void *arg);
avfs_device_driver avfs_netdisk_generic_write(avfs_device_major_number major, avfs_device_minor_number minor, void *arg);
avfs_device_driver net_rss_initialize(avfs_device_major_number major, avfs_device_minor_number minor, void *arg);
avfs_device_driver net_rss_ioctl(avfs_device_major_number major, avfs_device_minor_number minor, void *arg);

#ifndef HAS_FRAMECHANNEL_ONLY
avfs_device_driver net_fli_initialize(avfs_device_major_number major, avfs_device_minor_number minor, void *arg);
avfs_device_driver net_fli_ioctl(avfs_device_major_number major, avfs_device_minor_number minor, void *arg);
avfs_device_driver net_pic_initialize(avfs_device_major_number major, avfs_device_minor_number minor, void *arg);
avfs_device_driver net_pic_ioctl(avfs_device_major_number major, avfs_device_minor_number minor, void *arg);
#endif /* HAS_FRAMECHANNEL_ONLY */

avfs_device_driver net_frmch_initialize(avfs_device_major_number major, avfs_device_minor_number minor, void *arg);
avfs_device_driver net_frmch_ioctl(avfs_device_major_number major, avfs_device_minor_number minor, void *arg);
#endif /* ENABLE_NETWORK */


avfs_driver_address_table Device_drivers[] = {
#ifdef ENABLE_WATCHDOG
	DEVICE_TABLE_HW_WATCHDOG,
#endif
#ifdef ENABLE_NANDDRV	
	 NAND_DEVICE_ENTRY,
#endif

#if (defined AML_ARES)||(defined AML_COPIA)||(defined AML_NIKE)
#ifdef ARES_SOFT_I2C
     DEVICE_TABLE_SOFT_I2C,
#else     
     DEVICE_TABLE_HARD_I2C,
 #endif    
#else 
     DEVICE_TABLE_SOFT_I2C,
#endif 
    { hiu_initialize, hiu_open, hiu_close, NULL, NULL, hiu_control },
#ifdef ENABLE_ATAPI    
    {
     dvdrom_initialize,
      avfs_blkdev_generic_open, avfs_blkdev_generic_close, 
      avfs_blkdev_generic_read, avfs_blkdev_generic_write, 
      avfs_blkdev_generic_ioctl
    },
#endif
#if 1
    // audio interface 
    DEVICE_TABLE_AUDIO_INPUT,
    DEVICE_TABLE_AUDIO_OUTPUT,
    DEVICE_TABLE_AUDIO_AC3DEC,
    DEVICE_TABLE_AUDIO_LPCMDEC,
    DEVICE_TABLE_AUDIO_AVILPCMDEC,
    DEVICE_TABLE_AUDIO_MP2DEC,
    DEVICE_TABLE_AUDIO_MP3DEC,
//    DEVICE_TABLE_AUDIO_MP3DEC_C_VERSION,
//    DEVICE_TABLE_AUDIO_ALACDEC,
//    DEVICE_TABLE_AUDIO_FLACDEC,
#ifdef AML_NIKE
    DEVICE_TABLE_AUDIO_AACDEC,
#endif    
    DEVICE_TABLE_AUDIO_ADPCMDEC,
#ifdef SUPPORT_RM
    DEVICE_TABLE_AUDIO_RADEC,
    DEVICE_TABLE_AUDIO_RAACDEC,
#endif
#if (defined WISTAR_VERSION) ||(defined AML_NIKE)
//    DEVICE_TABLE_AUDIO_WMADEC,
#endif
    DEVICE_TABLE_SPECTRUM,
//    DEVICE_TABLE_AUDIO_OGGDEC,
//    DEVICE_TABLE_AUDIO_EQ,
//    DEVICE_TABLE_PROLOGIC2,
//    DEVICE_TABLE_BASS_MANAGEMENT,
#endif
    {displayctl_initialize,    NULL, NULL, NULL,NULL, displayctl_control },	
    {video_initialize,    NULL, NULL, NULL,NULL, video_control},	
    {jpegdec_initialize,    NULL, NULL, NULL,NULL, jpegdec_control},
    {hwjpegdec_initialize,    NULL, NULL, NULL,NULL, hwjpegdec_control},
#ifndef APPLY_8M_SDRAM
    {tiffdec_initialize,    NULL, NULL, NULL,NULL, tiffdec_control},
    {bmpdec_initialize,    NULL, NULL, NULL, bmpdec_write, bmpdec_control},
    {pngdec_initialize,    NULL, NULL, NULL, pngdec_write, pngdec_control},
    {gifdec_initialize,    NULL, NULL, NULL, gifdec_write, gifdec_control},
#endif
    {nulldec_initialize,    NULL, NULL, NULL,NULL, nulldec_control},
#ifdef ENABLE_MPEG2_DEC
    {mpeg2dec_initialize,    NULL, NULL, NULL,NULL, mpeg2dec_control},
#endif
#ifdef ENABLE_MPEG4_DEC
    {mpeg4dec_initialize,    NULL, NULL, NULL,NULL, mpeg4dec_control},
#endif
    {mjpegdec_initialize,    NULL, NULL, NULL,NULL, mjpegdec_control},
#ifdef SUPPORT_RM
   {realdec_initialize,    NULL, NULL, NULL,NULL, realdec_control},
#endif
    DEVICE_TABLE_OSD_OUTPUT,	
//    DEVICE_TABLE_OSD2_OUTPUT,
//    {osd_device_init, osd_device_open, osd_device_close, osd_device_read, osd_device_write,osd_device_ioctl }
#ifdef SUPPORT_KEYBOARD
    DEVICE_TABLE_KEYBOARD,
#endif
#ifdef SOFTWARE_MODE_REMOTE
	DEVICE_TABLE_NEC_SOFTWARE_MODE_REMOTE
#else
    DEVICE_TABLE_NEC_REMOTE
#endif
#ifdef ENABLE_DOSFS
    ,
    {
     ramdisk_initialize,
      avfs_blkdev_generic_open, avfs_blkdev_generic_close, 
      avfs_blkdev_generic_read, avfs_blkdev_generic_write, 
      avfs_blkdev_generic_ioctl
    }	
#endif
#ifdef ENABLE_CARD_READER
    ,
    {
      card_reader_initialize,
      avfs_blkdev_generic_open, avfs_blkdev_generic_close, 
      avfs_blkdev_generic_read, avfs_blkdev_generic_write, 
      avfs_blkdev_generic_ioctl
    }
#endif
#ifdef ENABLE_HDDRV
    ,
    {
     hddisk_initialize,
      avfs_blkdev_generic_open, avfs_blkdev_generic_close, 
      avfs_blkdev_generic_read, avfs_blkdev_generic_write, 
      avfs_blkdev_generic_ioctl
    }	
#endif
#ifdef ENABLE_USBDRV
    ,
    DEVICE_TABLE_USB_DEV
    ,
    {
     usb_ms_initialize,
      avfs_blkdev_generic_open, avfs_blkdev_generic_close, 
      avfs_blkdev_generic_read, avfs_blkdev_generic_write, 
      avfs_blkdev_generic_ioctl
    }	
#ifdef ENABLE_PTP
,
    USB_PTP_DRIVER_TABLE
#endif
#ifdef ENABLE_USBGADGET
    ,
	DEVICE_TALBE_USB_GADGET
#endif

#endif
#ifdef ENABLE_RS232
    ,
    DEVICE_TABLE_RS232
#endif


#ifndef ENABLE_NANDDRV
#ifdef ENABLE_SPIF_FLASH
	,
		SPI_DEVICE_ENTRY
#endif
#endif

#ifdef ENABLE_BLUETOOTH
	,
	DEVICE_TABLE_BLUETOOTH
	,
	DISK_TABLE_BLUETOOTH	
#endif
#ifdef ENABLE_NETWORK
    ,
    {
      net_rss_initialize,
      avfs_netdisk_generic_open, avfs_netdisk_generic_close, 
      avfs_netdisk_generic_read, avfs_netdisk_generic_write, 
      net_rss_ioctl
    }
    ,
    {
      net_fli_initialize,
      avfs_netdisk_generic_open, avfs_netdisk_generic_close, 
      avfs_netdisk_generic_read, avfs_netdisk_generic_write, 
      net_fli_ioctl
    }
    ,
    {
      net_pic_initialize,
      avfs_netdisk_generic_open, avfs_netdisk_generic_close, 
      avfs_netdisk_generic_read, avfs_netdisk_generic_write, 
      net_pic_ioctl
    }
    ,
#endif /* ENABLE_NETWORK_BASIC */

};


#define CONFIGURE_NUMBER_OF_DRIVERS \
  ((sizeof(Device_drivers) / sizeof(avfs_driver_address_table)))

#ifndef CONFIGURE_MAXIMUM_DRIVERS
#define CONFIGURE_MAXIMUM_DRIVERS CONFIGURE_NUMBER_OF_DRIVERS
#endif

/*
 *  Default the number of devices per device driver.  This value may be
 *  overridden by the user.
 */

#ifndef CONFIGURE_MAXIMUM_DEVICES
#define CONFIGURE_MAXIMUM_DEVICES   20
#endif

avfs_configuration_table IO_CONFIGURATION={
  CONFIGURE_MAXIMUM_DEVICES,
  CONFIGURE_MAXIMUM_DRIVERS,
  CONFIGURE_NUMBER_OF_DRIVERS,               /* number of device drivers */
  Device_drivers                    /* pointer to driver table */
};	

